﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PClusterModel.Infrastructure;
using System.Collections;

namespace PClusterModel.Cluster
{
    internal class MaximalDimensionSetFinderScaling:MaximalDimensionSetFinder
    {
        public MaximalDimensionSetFinderScaling(PClusterizer clusterizer)
            :base(clusterizer)
        {
            this.realTrashold = (float)trashold / 100;
        }

        private float realTrashold;

        protected override MaximalDimensionBlockSet FindMaximalDimensionSet(IMicroarrayDataObject firstObject, IMicroarrayDataObject secondObject)
        {
            List<KeyValuePair<int, float>> substractedValues = new List<KeyValuePair<int, float>>();
            int conditionCount = firstObject.ExpresionValues.Count;

            for (int i = 0; i < conditionCount; i++)
            {
                float firstValue = firstObject.ExpresionValues[i];
                float secondValue = secondObject.ExpresionValues[i];

                if (firstValue == 0 || secondValue == 0)
                    continue;

                float difference = this.CalculateDifference(secondValue, firstValue);
                substractedValues.Add(new KeyValuePair<int, float>(i, difference));
            }

            substractedValues.Sort(new PClusterModel.Comparer.KeyValuePairComparerByValue<int, float>());
            IList<BitArray> maximalDimensionSetBitmaps = this.ExtractMaximalDimensionSetBitMaps(substractedValues);
            if (maximalDimensionSetBitmaps.Count > 0)
            {
                MaximalDimensionBlockSet maxDimensionSet = new MaximalDimensionBlockSet(firstObject, secondObject);
                maxDimensionSet.ConditionBitMaps.AddRange(maximalDimensionSetBitmaps);
                return maxDimensionSet;
            }
            return null;
        }

        protected override bool CheckDifference(float difference)
        {
            return difference <= 1 + realTrashold && difference >= 1 - realTrashold;
        }

        protected override float CalculateDifference(float first, float second)
        {
            return first / second;
        }
    }
}
